<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>验证码登录</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
          integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"
            integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ"
            crossorigin="anonymous"></script>

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"
            integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd"
            crossorigin="anonymous"></script>
</head>
<body background="/img/login.jpg" style="background-repeat: no-repeat;background-size: cover">
<div class="container">
    <div class="row" style="margin-top: 200px;">
        <div class="col-md-4 col-md-offset-4">
            <div style="margin:10px 10px 10px 10px">

                <!-- 输入手机号-->
                <div id="phone_div" class="form-group input-group-lg">
                    <label style="font-size: 16px;" class="control-label">手机号</label>
                    <input id="phone" type="tel" class="form-control" placeholder="请输入手机号">
                    <span id="message1" class="help-block" style="height: 12px;"></span>
                </div>

                <div class="form-group" id="code_div">
                    <!-- 输入验证码 -->
                    <label style="font-size: 16px;" class="control-label">验证码</label>
                    <div class="input-group input-group-lg">
                        <input id="code" type="text" class="form-control" placeholder="请输入验证码">
                        <span class="input-group-btn">
                <button id="code_btn" class="btn btn-default" type="button">获取</button>
              </span>
                    </div>
                    <span id="message2" class="help-block" style="height: 12px;"></span>
                </div>
                <button id="sub_btn" type="submit" class="btn btn-default btn-lg btn-block">登录/注册</button>
                <br>
                <div class="row">
                    <div class="col-md-4">
                        <a href="index.jsp" class="button"><span
                                class="glyphicon glyphicon-grain glyphicon glyphicon-">返回主页</span></a>
                    </div>
                    <div class="col-md-4">
                        <a href="enroll.jsp" class="button"><span
                                class="glyphicon glyphicon-grain glyphicon glyphicon-">用户注册</span></a>
                    </div>
                    <div class="col-md-4">
                        <a href="login2.jsp" class="button"><span
                                class="glyphicon glyphicon-grain glyphicon glyphicon-">密码登录</span></a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    $(document).ready(function () {


        //验证码倒计时参数变量
        var time = 60;
        var timer;
        //获取验证码按钮点击，触发该事件
        $("#code_btn").click(function () {
            //验证手机号格式
            var phone = $("#phone").val();
            var regex = /^(13[0-9]{9})|(15[0-9]{9})|(17[0-9]{9})|(18[0-9]{9})|(19[0-9]{9})$/;
            if (phone == null || phone == "") {
                $("#phone_div").addClass("has-error");
                $("#message1").text("手机号不能为空");
                $('#phone').focus();
                return;
            }
            if (!phone.match(regex)) {
                $("#phone_div").addClass("has-error");
                $("#message1").text("手机号格式错误");
                $('#phone').focus();
                return;
            }
            //手机号不为空，并且格式也正确，则倒计时禁用按钮并发送验证码
            $("#code_btn").prop("disabled", true);
            //每次发送短信验证码时，需重置计时器时间
            time = 60;
            //先检测手机号是否注册过
            $.get("/user/checkPhone?phone=" + phone, function (result) {
                if (result=="phoneExist"){
                    //请求发送验证码
                    $.get("/user/sendLoginCode?loginPhone="+phone)
                }
            });
            //定时器
            timer = setInterval(timeDown, 1000);
        });

        //计时器函数
        function timeDown() {
            time--;
            if (time <= 0) {
                //清除计时器
                clearInterval(timer);
                //解开按钮
                $("#code_btn").prop("disabled", false);
                $("#code_btn").text("获取");
            } else {
                $("#code_btn").text("重新发送：（" + time + "s）");
            }
        }

        //恢复输入框状态
        //用户点击输入手机号时，恢复输入框状态
        $("#phone").click(function () {
            $("#phone_div").removeClass("has-error");
            $("#message1").text("");
        });
        //用户点击输入验证码时，恢复输入框状态
        $("#code").click(function () {
            $("#code_div").removeClass("has-error");
            $("#message2").text("");
        });


        //用户点击登录\注册按钮
        $("#sub_btn").click(function () {
            //检验手机号
            var phone = $("#phone").val();
            var regex = /^(13[0-9]{9})|(15[0-9]{9})|(17[0-9]{9})|(18[0-9]{9})|(19[0-9]{9})$/;
            if (phone == null || phone == "") {
                $("#phone_div").addClass("has-error");
                $("#message1").text("手机号不能为空");
                return;
            }
            if (!phone.match(regex)) {
                $("#phone_div").addClass("has-error");
                $("#message1").text("手机号格式错误");
                $('#phone').focus();
                return;
            }
            $("#phone_div").removeClass("has-error");
            //判断用户是否输入验证码
            var code = $("#code").val();
            if (code != null && code != "") {
                //发送验证码给服务端进行验证码的校验，若校验成功则登录成功
                $.get("/user/loginByCode","loginPhone="+phone+"&loginCode="+code, function (result) {
                    if (result=="notPhone"){
                        $("#phone_div").addClass("has-error");
                        $("#message1").text("手机号不合法");
                        return;
                    }else if (result=="difPhone") {
                        $("#phone_div").addClass("has-error");
                        $("#message1").text("前后手机号不一致");
                        return;
                    }else if (result=="errorCode"){
                        $("#code_div").addClass("has-error");
                        $("#message2").text("验证码错误错误");
                        return;
                    }else if (result=="phoneNotExist"){
                        $("#phone_div").addClass("has-error");
                        $("#message1").text("该手机号尚未注册");
                        return;
                    }
                    location.replace("/note/getNotesByPage");
                });
            } else {
                $("#code_div").addClass("has-error");
                $("#message2").text("验证码不能为空");
                return;
            }
        });

    });
</script>

</html>